package com.ossjk.core.base.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.ossjk.core.constant.Constant;
import com.ossjk.core.util.JwtTokenUtil;
import com.ossjk.core.vo.ResponseBean;

public class BaseController {

	public Logger log = LoggerFactory.getLogger(BaseController.class);

	@Autowired
	private JwtTokenUtil jwtTokenUtil;

	/**
	 * 得到request对象
	 */
	protected HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 获取token
	 */
	protected String getToken() {
		return jwtTokenUtil.getToken(getRequest());
	}

	/**
	 * 获取用户ID
	 */
	protected String getUid() {
		return (String) getRequest().getAttribute(Constant.REQUEST_ATTRIBUTE_UID);
	}

	/**
	 * 全局异常捕捉
	 *
	 * @param e
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@ExceptionHandler(Exception.class)
	public Object exception(Exception e, HttpServletRequest request, HttpServletResponse response) throws IOException {
		e.printStackTrace();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		ResponseBean responseBean = new ResponseBean(Constant.RESPONSE_CODE_EXCEPTION, Constant.RESPONSE_MSG_EXCEPTION, null);
		out.write(JSON.toJSONString(responseBean));
		out.flush();
		out.close();
		return null;
	}

	/**
	 * 没有权限异常捕捉
	 *
	 * @param e
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@ExceptionHandler(UnauthorizedException.class)
	public Object UnauthorizedException(Exception e, HttpServletRequest request, HttpServletResponse response) throws IOException {
		e.printStackTrace();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		ResponseBean responseBean = new ResponseBean(Constant.RESPONSE_CODE_NOAUTH, Constant.RESPONSE_MSG_NOAUTH, null);
		out.write(JSON.toJSONString(responseBean));
		out.flush();
		out.close();
		return null;
	}



	 

	/**
	 * 响应成功
	 *
	 * @return
	 */
	protected ResponseBean responseSuccess() {
		return new ResponseBean(Constant.RESPONSE_CODE_SUCCESS, Constant.RESPONSE_MSG_SUCCESS, null);
	}

	/**
	 * 响应成功
	 *
	 * @param msg 响应信息
	 * @return
	 */
	protected ResponseBean responseSuccess(String msg) {
		return new ResponseBean(Constant.RESPONSE_CODE_SUCCESS, msg, null);
	}

	/**
	 * 响应成功
	 *
	 * @param data 响应内容
	 * @return
	 */
	protected ResponseBean responseSuccess(Object data) {
		return new ResponseBean(Constant.RESPONSE_CODE_SUCCESS, Constant.RESPONSE_MSG_SUCCESS, data);
	}

	/**
	 * 响应成功
	 *
	 * @param msg  响应信息
	 * @param data 响应内容
	 * @return
	 */
	protected ResponseBean responseSuccess(String msg, Object data) {
		return new ResponseBean(Constant.RESPONSE_CODE_SUCCESS, msg, data);
	}

	/**
	 * 响应失败
	 *
	 * @return
	 */
	protected ResponseBean responseFail() {
		return new ResponseBean(Constant.RESPONSE_CODE_FAIL, Constant.RESPONSE_MSG_FAIL, null);
	}

	/**
	 * 响应失败
	 *
	 * @param msg 响应信息
	 * @return
	 */
	protected ResponseBean responseFail(String msg) {
		return new ResponseBean(Constant.RESPONSE_CODE_FAIL, msg, null);
	}

	/**
	 * 响应失败
	 *
	 * @param data 响应内容
	 * @return
	 */
	protected ResponseBean responseFail(Object data) {
		return new ResponseBean(Constant.RESPONSE_CODE_FAIL, Constant.RESPONSE_MSG_FAIL, data);
	}

	/**
	 * 响应失败
	 *
	 * @param msg  响应信息
	 * @param data 响应内容
	 * @return
	 */
	protected ResponseBean responseFail(String msg, Object data) {
		return new ResponseBean(Constant.RESPONSE_CODE_FAIL, msg, data);
	}

	/**
	 * 响应报错
	 *
	 * 
	 * @return
	 */
	protected ResponseBean responseException() {
		return new ResponseBean(Constant.RESPONSE_CODE_EXCEPTION, Constant.RESPONSE_MSG_EXCEPTION, null);
	}

	/**
	 * 响应报错
	 *
	 * @param msg 响应信息
	 * @return
	 */
	protected ResponseBean responseException(String msg) {
		return new ResponseBean(Constant.RESPONSE_CODE_EXCEPTION, msg, null);
	}

	/**
	 * 响应报错
	 *
	 * @param data 响应内容
	 * @return
	 */
	protected ResponseBean responseException(Object data) {
		return new ResponseBean(Constant.RESPONSE_CODE_EXCEPTION, Constant.RESPONSE_MSG_EXCEPTION, data);
	}

	/**
	 * 响应报错
	 *
	 * @param msg  响应信息
	 * @param data 响应内容
	 * @return
	 */
	protected ResponseBean responseException(String msg, Object data) {
		return new ResponseBean(Constant.RESPONSE_CODE_EXCEPTION, msg, data);
	}

}
